package com.app.motion.parallaximageviewmotion;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;

public class ParallaxImageView extends AppCompatImageView implements MainActivity.OnMyScrollListener {

    private final String TAG = this.getClass().getSimpleName();


    public ParallaxImageView(Context context) {
        this(context, null);
    }

    public ParallaxImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ParallaxImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        this.setScaleType(ScaleType.MATRIX);
    }


    private float computeScale() {
        int viewWidth = getWidth() - getPaddingLeft() - getPaddingRight();
        int viewHeight = getHeight() - getPaddingTop() - getPaddingBottom();
        int drableWidth = this.getDrawable().getIntrinsicWidth();
        int drawableHeight = this.getDrawable().getMinimumHeight();
        float scale = 1.0f;
        if (viewWidth * drawableHeight > viewHeight * drableWidth) {
            scale = viewWidth * 1.0f / drableWidth;
        } else {
            scale = viewHeight * 1.0f / drawableHeight;
        }
        return scale;
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Matrix matrix = this.getImageMatrix();
        float scale = computeScale();
        if (scale != 1) {
            matrix.setScale(scale, scale);
        }
        this.setImageMatrix(matrix);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    public void onMyScroll(AbsListView listView, View itemView) {
        int listViewHeight = listView.getHeight();
        int itemViewPosition = itemView.getBottom() - itemView.getHeight() / 2;
        //计算itemview在listview中的位置
        int delta = itemViewPosition - listViewHeight / 2;
        float scale = Math.max(0, 1 - delta * 2.0f / listViewHeight); // 0~2
        doTransition(scale, itemView.getHeight());
    }

    private void doTransition(float scale, int itemHeight) {
        Log.e("wanghaha", "scale:" + scale);
        float initScale = computeScale();
        Matrix currentMatrix = this.getImageMatrix();
        currentMatrix.reset();
        if (scale != 1) {
            currentMatrix.setScale(initScale, initScale);
        }
        int drawableHeight = (int) (this.getDrawable().getIntrinsicHeight() * initScale);
        int middle = (drawableHeight - itemHeight) / 2;
        float maxTransition = Math.max(0, drawableHeight - itemHeight);
        int currentTransiton = (int) Math.min(maxTransition, middle * scale);
        currentMatrix.postTranslate(0, -currentTransiton);
        this.setImageMatrix(currentMatrix);
        invalidate();
    }
}

